package com.ytzl.itrip.trade.service.impl;

import com.cloopen.rest.sdk.utils.PropertiesUtil;
import com.ytzl.itrip.beans.model.ItripTradeEnds;
import com.ytzl.itrip.dao.mapper.ItripHotelOrderMapper;
import com.ytzl.itrip.beans.model.ItripHotelOrder;
import com.ytzl.itrip.dao.mapper.ItripTradeEndsMapper;
import com.ytzl.itrip.trade.service.ItripHotelOrderService;
import com.ytzl.itrip.utils.common.EmptyUtils;
import com.ytzl.itrip.utils.common.Page;
import com.ytzl.itrip.utils.common.PropertiesUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ytzl.itrip.utils.common.Constants;

@Service("itripHotelOrderService")
public class ItripHotelOrderServiceImpl implements ItripHotelOrderService {

    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;

    @Resource
    private ItripTradeEndsMapper itripTradeEndsMapper;

    public ItripHotelOrder getItripHotelOrderById(Long id) throws Exception {
        return itripHotelOrderMapper.getItripHotelOrderById(id);
    }

    public List<ItripHotelOrder> getItripHotelOrderListByMap(Map<String, Object> param) throws Exception {
        return itripHotelOrderMapper.getItripHotelOrderListByMap(param);
    }

    public Integer getItripHotelOrderCountByMap(Map<String, Object> param) throws Exception {
        return itripHotelOrderMapper.getItripHotelOrderCountByMap(param);
    }

    public Integer saveItripHotelOrder(ItripHotelOrder itripHotelOrder) throws Exception {
        itripHotelOrder.setCreationDate(new Date());
        return itripHotelOrderMapper.saveItripHotelOrder(itripHotelOrder);
    }

    public Integer modifyItripHotelOrder(ItripHotelOrder itripHotelOrder) throws Exception {
        itripHotelOrder.setModifyDate(new Date());
        return itripHotelOrderMapper.modifyItripHotelOrder(itripHotelOrder);
    }

    public Integer removeItripHotelOrderById(Long id) throws Exception {
        return itripHotelOrderMapper.removeItripHotelOrderById(id);
    }

    public Page<ItripHotelOrder> queryItripHotelOrderPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = itripHotelOrderMapper.getItripHotelOrderCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<ItripHotelOrder> itripHotelOrderList = itripHotelOrderMapper.getItripHotelOrderListByMap(param);
        page.setRows(itripHotelOrderList);
        return page;
    }

    @Override
    public Boolean processed(String orderNo) throws Exception {
        ItripHotelOrder itripHotelOrder = loadItripHotelOrderByOrderNo(orderNo);
        if (null != itripHotelOrder && itripHotelOrder.getOrderStatus() == 2
                && EmptyUtils.isNotEmpty(itripHotelOrder.getTradeNo())) {
            return true;
        }
        return false;
    }

    @Override
    public ItripHotelOrder loadItripHotelOrderByOrderNo(String orderNo) throws Exception {
        //通过订单编号查询订单信息
        Map<String, Object> param = new HashMap();
        param.put("orderNo", orderNo);
        List<ItripHotelOrder> itripHotelOrderList = getItripHotelOrderListByMap(param);
        if (null != itripHotelOrderList && itripHotelOrderList.size() > 0) {
            ItripHotelOrder itripHotelOrder = itripHotelOrderList.get(0);
            return itripHotelOrder;
        }
        return null;
    }

    @Override
    public void updateItripHotelOrderPayFail(String orderNo, String tradeNo) throws Exception {
        ItripHotelOrder itripHotelOrder = loadItripHotelOrderByOrderNo(orderNo);
        if (null != itripHotelOrder) {
            //更新订单状态
            itripHotelOrder.setPayType(1);
            itripHotelOrder.setOrderStatus(1);
            itripHotelOrder.setTradeNo(tradeNo);
            this.modifyItripHotelOrder(itripHotelOrder);
        }
    }

    @Override
    public void updateItripHotelOrderPaySuccess(String orderNo, String tradeNo) throws Exception {
        ItripHotelOrder itripHotelOrder = loadItripHotelOrderByOrderNo(orderNo);
        if (null != itripHotelOrder) {
            //更新订单状态
            itripHotelOrder.setPayType(1);
            itripHotelOrder.setOrderStatus(2);
            itripHotelOrder.setTradeNo(tradeNo);
            this.modifyItripHotelOrder(itripHotelOrder);
            //订单记录
            ItripTradeEnds itripTradeEnds = new ItripTradeEnds();
            itripTradeEnds.setId(itripHotelOrder.getHotelId());
            itripTradeEnds.setFlag("0");
            itripTradeEnds.setOrderNo(orderNo);
            itripTradeEndsMapper.saveItripTradeEnds(itripTradeEnds);
            //库存减少
            //获取访问地址
            String scanTradeEndUrl = PropertiesUtils.get("public.properties",
                    "scanTradeEndUrl");
            executeGet(String.format(scanTradeEndUrl, orderNo));
        }
    }

    public void executeGet(String urlStr) {
        try {
            URL url = new URL(urlStr);
            //创建连接
            URLConnection connection = url.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "keep-alive");
            connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36");
            //进行访问
            connection.connect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
